package cn.marinda.resturants.controller;

import cn.marinda.resturants.entity.User;
import cn.marinda.resturants.enums.EntityJson;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.UserMapper;
import cn.marinda.resturants.service.UserService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.IOUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** User控制器
 * @date 2021/11/28
 * @author Marinda
 *
 */
@Api(value = "生产者进程")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;



    /** 修改用户
     *
     * @param user  用户
     * @return 修改结果
     */
    @ApiOperation(value = "修改用户",notes = "修改",produces = "application/json")
    @RequestMapping("/update")
    @ApiImplicitParam(name = "user",value = "参数",paramType = "query",required = true,dataType = "String")
    public ApiResult update(User user){
        return userService.update(user) ?  new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /** 添加用户
     *
     * @param user 用户
     * @return 是否成功
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "user",required = true,value = "用户实体类",dataType = "User"),@ApiImplicitParam(name = "uid",value = "用户唯一标识"),@ApiImplicitParam(name = "phone",value = "手机号"),@ApiImplicitParam(name = "email",value = "邮箱"),@ApiImplicitParam(name = "memberUid",value = "会员uid"),@ApiImplicitParam(name = "LoginDate",value = "登录时间"),@ApiImplicitParam(name = "name",value = "用户名"),@ApiImplicitParam(name = "password",value = "密码"),@ApiImplicitParam(name = "version",value = "版本号"),@ApiImplicitParam(name = "vochers",value = "礼券数")})
    @ApiOperation(value = "添加",notes = "User.add方法")
    @ApiResponses( {@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 400,message = "失败")})
    @RequestMapping("/add")
    private ApiResult addUser(User user){
        return userService.addUser(user) ?  new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /** 为前端页面实现分类必要参数实现
     * @param limit 数量
     * @page 页数
     * @return 是否成功
     */
    @RequestMapping("/pages")
    private HashMap selectPages(@RequestParam("page") int page, @RequestParam("limit") int limit){

        IPage<User> iPage = userService.selectPageList(new Pages(page,limit));
        Map<String,Object> pageMap = new HashMap<String,Object>();
        pageMap.put("count",iPage.getTotal());
        pageMap.put("data",iPage.getRecords());
        pageMap.put("code",0);
        return (HashMap) pageMap;
    }

    /** 删除用户
     *
     * @param uid 用户
     * @return 是否成功
     */
    @RequestMapping("/delete")
    private ApiResult deleteUser(String uid){
        return userService.deleteUser(uid) ?  new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }

    /** 查询所有用户信息
     *
     * @return 用户信息列表
     */
    @RequestMapping("/queryList")
    private ApiResult queryList(){
        int counts = userService.queryList().size();
        ApiResult apiResult = new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,userService.queryList());
        apiResult.setCount(counts);
        return apiResult;

    }


    /** 通过uid来查询一个用户
     *
     *
     * @param userUid 用户uid
     * @return 用户
     */
    @RequestMapping("/index")
    private ApiResult selectUserUid(@RequestParam("userUid") String userUid){
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE, Arrays.asList(userService.selectUserUid(userUid)));

    }


    /** 模糊查询名字存在name的用户
     *
     * @date 2021/11/28
     * @param name 名
     * @return 用户
     */
    @RequestMapping("/like")
    private ApiResult selectLikeName(String name){
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,userService.selectLikeName(name));

    }

    /** 返回一个指定日期的登录时间的所有用户
     *
     * @param startDate startDate
     * @param endDate 结束时间
     * @return  用户列表
     */
    @RequestMapping("/loginDate")
    private ApiResult selectLoginDate(String startDate,String endDate){
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,userService.selectLoginDate(startDate, endDate));

    }


    /** 返回一个指定日期的注册时间的所有用户
     *
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 用户的集合
     */
    @RequestMapping("/registerDate")
    private ApiResult selectRegisterDate(String startDate,String endDate){
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,userService.selectRegisterDate(startDate, endDate));

    }


    /** 在手机号唯一性给一个用户绑定的情况下
     *
     * @param phone
     * @return 用户
     */
    @RequestMapping("/phone")
    private ApiResult selectPhone(String phone){
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,userService.selectPhone(phone));

    }

    /** 查询属于某一会员的用户列表
     *
     * @param uid 用户uid
     * @return 用户集合
     */
    @RequestMapping("/member")
    private ApiResult selectMember(int uid){
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,userService.selectMember(uid));
    }

}

